package org.example.skytool.mq.rabbitmq;

import lombok.extern.slf4j.Slf4j;
import org.example.annotation.LogTrace;
import org.example.config.RabbitMqConfig;
import org.example.dao.dao.post.PPostDao;
import org.example.dao.dto.post.PostCacheDto;
import org.example.dao.dto.post.PostRedisDto;
import org.example.dao.entity.post.PPost;
import org.example.skytool.json.JackSonUtil;
import org.example.skytool.redis.RedisUtils;
import org.example.utils.post.post.RedisPostService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 22449
 */
@Service
@Slf4j
public class PostCacheReceiver {

    @Resource
    private PPostDao pPostDao;

    @Resource
    private RedisPostService redisPostService;

    @Resource
    private RedisUtils redisUtils;

    @LogTrace
    @RabbitListener(queues = RabbitMqConfig.POST_QUEUE)
    public void receive(String message) {
        PostCacheDto postCacheDto = JackSonUtil.parse(PostCacheDto.class,message);
        if(postCacheDto==null){
            log.error("post mq数据存储错误");
            return;
        }
        Long postId = postCacheDto.getPostId();
        PPost post = pPostDao.selectById(postId);
        PostRedisDto postRedisDto = redisPostService.getCacheInfo(postId.toString());
        postRedisDto.setCacheData(post);
        pPostDao.updateById(post);
    }

}
